package com.ext.aic.box.wx;

import android.text.TextUtils;

import com.ext.aic.AICManager;
import com.ext.aic.beans.box.ChatWindowElement;
import com.ext.aic.beans.box.ChatWindowElement_;
import com.ext.aic.beans.box.ConversationElement;
import com.ext.aic.beans.box.ConversationElement_;
import com.ext.aic.beans.box.ConversationWindowElement;
import com.ext.aic.beans.box.ConversationWindowElement_;
import com.ext.aic.beans.box.NotificationMessage;
import com.ext.aic.beans.box.NotificationMessage_;
import com.ext.aic.beans.box.PageTabElement;
import com.ext.aic.beans.box.PageTabElement_;
import com.eyinfo.mpkv.MPLocalKV;

import java.util.List;

import io.objectbox.query.QueryBuilder;

public class WxDbHelper {

    private String currentVersionKey = "c0f4562918dbd433";
    private String currentVersion;
    private PageTabElement pageTabElement;
    private ConversationWindowElement conversationWindowElement;
    private ChatWindowElement chatWindowElement;
    private String newMessageMarkId;

    public String getCurrentVersion() {
        if (TextUtils.isEmpty(currentVersion)) {
            currentVersion = MPLocalKV.getInstance().getString(currentVersionKey, "");
        }
        return currentVersion;
    }

    public void saveCurrentVersion(String version) {
        if (TextUtils.isEmpty(currentVersion)) {
            currentVersion = version;
            MPLocalKV.getInstance().put(currentVersionKey, version);
        }
    }

    public PageTabElement findPageTabElement(String version) {
        if (pageTabElement == null) {
            pageTabElement = AICManager.getInstance().findOne(PageTabElement.class, queryBuilder -> {
                queryBuilder.equal(PageTabElement_.version, version, QueryBuilder.StringOrder.CASE_INSENSITIVE);
            });
        }
        return pageTabElement;
    }

    public ConversationWindowElement findConversationWindowElement(String version) {
        if (conversationWindowElement == null) {
            conversationWindowElement = AICManager.getInstance().findOne(ConversationWindowElement.class, queryBuilder -> {
                queryBuilder.equal(ConversationWindowElement_.version, version, QueryBuilder.StringOrder.CASE_INSENSITIVE);
            });
        }
        return conversationWindowElement;
    }

    public ChatWindowElement findChatWindowElement(String version) {
        if (chatWindowElement == null) {
            chatWindowElement = AICManager.getInstance().findOne(ChatWindowElement.class, queryBuilder -> {
                queryBuilder.equal(ChatWindowElement_.version, version, QueryBuilder.StringOrder.CASE_INSENSITIVE);
            });
        }
        return chatWindowElement;
    }

    public List<NotificationMessage> findStayReplyMessageList() {
        return AICManager.getInstance().findList(NotificationMessage.class, queryBuilder -> {
            long earliestTime = System.currentTimeMillis() - 300000;

            queryBuilder.equal(NotificationMessage_.status, 0);
            queryBuilder.notEqual(NotificationMessage_.message, "", QueryBuilder.StringOrder.CASE_INSENSITIVE);
            queryBuilder.greater(NotificationMessage_.receiveTime, earliestTime);
            queryBuilder.orderDesc(NotificationMessage_.receiveTime);
        });
    }

    //如果超过5分钟仍未回复，则视为无效消息直接删除
    public void removeInvalidMessage() {
        AICManager.getInstance().delete(NotificationMessage.class, queryBuilder -> {
            long earliestTime = System.currentTimeMillis() - 300000;
            queryBuilder.lessOrEqual(NotificationMessage_.receiveTime, earliestTime);
        });
    }

    //删除消息
    public void deleteMessageById(long id) {
        AICManager.getInstance().delete(NotificationMessage.class, queryBuilder -> {
            queryBuilder.equal(NotificationMessage_.id, id);
        });
    }

    public String getNewMessageMarkId() {
        if (TextUtils.isEmpty(newMessageMarkId)) {
            String version = getCurrentVersion();
            ConversationElement element = AICManager.getInstance().findOne(ConversationElement.class, queryBuilder -> {
                queryBuilder.equal(ConversationElement_.version, version, QueryBuilder.StringOrder.CASE_INSENSITIVE);
            });
            if (element != null && !TextUtils.isEmpty(element.getNewMsgMarkId())) {
                newMessageMarkId = element.getNewMsgMarkId();
            }
        }
        return newMessageMarkId;
    }
}
